package gemini.castor.ui.client.page.content.home.apply.contractagreement;

import gemini.basic.model.Distributor;
import gemini.basic.model.Person;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.home.apply.ApplyEvent;
import gemini.castor.ui.client.page.content.home.apply.checkout.CheckOutFormObject;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;
import gemini.castor.ui.client.utils.UiConstants;

import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class ContractAgreementFormPresenter extends
		CastorWidgetPresenter<ContractAgreementFormPresenter.Display> {

	private Distributor inputObject;
	
	@Inject
	public ContractAgreementFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getAgreementSubmitClickHandlers();
		HasClickHandlers getBackClickHandlers();
		HasClickHandlers getCancelClickHandlers();
		HasClickHandlers getPrintClickHandlers();
		void setPersons(List<Person> persons);
		String getFullName();
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_REGISTER);

	@Override
	protected void onBind() {

		registerHandler(display.getAgreementSubmitClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						saveDistributor();
					}
				}));
		registerHandler(display.getBackClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new ApplyEvent(
								ApplyEvent.ForwardType.BACK));
					}
				}));
		registerHandler(display.getCancelClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new ApplyEvent(
								ApplyEvent.ForwardType.CANCEL));
					}
				}));
		registerHandler(display.getPrintClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						GuiUtils.alert("Printer is not ready!");
					}
				}));
		
		display.setPersons(inputObject.getPersons());
		display.loadData();
	}
	
	@Override
	public void setInputObject(Object object){
		inputObject = (Distributor) object;
	}
	
	private Object outputObject;
	
	@Override
	public Object getOutputObject() {
		return outputObject;
	}
	
	private void saveDistributor() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getDistributorService().update(inputObject,
				new AbstractAsyncCallback<Distributor>() {

					@Override
					public void onSuccess(Distributor result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						GuiUtils.info("Saving distributor successfull!!<br />Please remember your Distributor Id <" + result.getCode() + "> and Pin!");
						
						CheckOutFormObject checkOutObject = new CheckOutFormObject();
						checkOutObject.setPaymentValue(UiConstants.APPLY_PRICE);
						checkOutObject.setDistributorCode(inputObject.getDistributor().getCode());
						checkOutObject.setFullName(display.getFullName());
						outputObject = checkOutObject;
						
						localEventBus.fireEvent(new ApplyEvent(
								ApplyEvent.ForwardType.AGREEMENT_SUBMIT));
					}
				});

	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
}
